package com.woniuxy.lab.woniu_lab.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.lab.woniu_lab.dao.RangeDao;
import com.woniuxy.lab.woniu_lab.exception.RangeException;
import com.woniuxy.lab.woniu_lab.exception.WebException;
import com.woniuxy.lab.woniu_lab.model.Mark;
import com.woniuxy.lab.woniu_lab.dao.MarkDao;
import com.woniuxy.lab.woniu_lab.model.Range;
import com.woniuxy.lab.woniu_lab.model.User;
import com.woniuxy.lab.woniu_lab.service.MarkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lab.woniu_lab.service.RangeService;
import com.woniuxy.lab.woniu_lab.service.UserService;
import com.woniuxy.lab.woniu_lab.service.dto.markDto.UserMarkDto;
import com.woniuxy.lab.woniu_lab.util.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.woniuxy.lab.woniu_lab.exception.WebExceptionCode.RANGE_NOT_EXISTS;
import static com.woniuxy.lab.woniu_lab.exception.WebExceptionCode.YOU_ALREADY_COLLECT_THIS_RANGE;

/**
 * <p>
 * 用户收藏表 服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-03-15
 */
@Service
public class MarkServiceImpl extends ServiceImpl<MarkDao, Mark> implements MarkService {

    public static final String DISABLE = "DISABLE";
    @Resource
    private MarkDao markDao;

    @Resource
    private RangeDao rangeDao;
    @Resource
    private UserService userService;
    @Resource
    private RangeService rangeService;

    @Override
    public IPage<UserMarkDto> queryMyMark(IPage<UserMarkDto> iPage) {
        //判断用户
        User user = userService.assertUser();
        //查询
        return rangeDao.queryMyMark(user.getId(), iPage);
    }

    @Override
    public IPage<UserMarkDto> queryMyCTF(IPage<UserMarkDto> iPage) {
        //判断用户
        User user = userService.assertUser();
        //查询
        return rangeDao.queryMyCTF(user.getId(), iPage);
    }

    @Override
    public void delCollectRange(Integer rangeId) {
        //判断用户
        User user = userService.assertUser();
        //判断是否存在
//        Mark mark = getById(rangeId);
        Mark mark = markDao.selectOne(new QueryWrapper<Mark>().eq("range_id", rangeId).eq("user_id", user.getId()));
        if (mark == null) throw new WebException(400, "该实验不存在");
        //删除
        removeById(mark);
    }

    @Override
    public void collectRange(Integer userId, Integer rangeId) {
        //查询靶场及验证靶场
        Range range = rangeService.getById(rangeId);
        if (ObjectUtil.isEmpty(range) || range.getStatus().equals(DISABLE)) {
            throw new RangeException(RANGE_NOT_EXISTS);
        }
        //查询用户
        Mark mark1 = markDao.selectOne(new QueryWrapper<Mark>().eq("user_id", userId).eq("range_id", rangeId));
        if (mark1 != null) throw new RangeException(YOU_ALREADY_COLLECT_THIS_RANGE);

        Mark mark = new Mark();
        mark.setRangeId(range.getId());
        mark.setUserId(userId);
        mark.setExperName(range.getExperName());
        save(mark);


    }
}
